package com.itmuch.core.converter;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.itmuch.core.constant.ConstantCode;

/**
 * 返回结果包装类.
 * 
 * @author Zhouli
 *
 */
public class AjaxResult<T> {
	private static final Logger LOGGER = LoggerFactory.getLogger(AjaxResult.class);

	private boolean success;
	private long code;
	private String title;
	private String content;
	private T data;

	public AjaxResult() {
	}

	public AjaxResult(boolean success, long code, String title, String content, T data) {
		this.success = success;
		this.code = code;
		this.title = title;
		this.content = content;
		this.data = data;
	}

	public AjaxResult<T> success(String title, String content) {
		this.success = true;
		this.code = ConstantCode.SUCCESS_CODE;

		this.content = content;
		this.title = title;
		return this;
	}

	public AjaxResult<T> success(T data) {
		this.data = data;
		this.success = true;
		this.code = ConstantCode.SUCCESS_CODE;
		this.content = "操作成功";
		this.title = "成功";
		return this;
	}

	public AjaxResult<T> error(String title, String content, long errorCode) {
		this.success = false;
		this.code = errorCode;
		this.title = title;
		this.content = content;
		this.data = null;
		return this;
	}
	
	public AjaxResult<T> error(long errorCode) {
		this.success = false;
		this.code = errorCode;
		this.title = "错误发生";
		this.content = "错误";
		this.data = null;
		return this;
	}

	public AjaxResult<T> jsrValidError(BindingResult bindingResult) {
		Map<String, List<String>> fieldErrors = Maps.newHashMap();
		for (FieldError fe : bindingResult.getFieldErrors()) {
			String f = fe.getField();

			if (fieldErrors.get(f) != null) {
				fieldErrors.get(f).add(fe.getDefaultMessage());
			} else {
				List<String> list = new LinkedList<String>();
				list.add(fe.getDefaultMessage());
				fieldErrors.put(f, list);
			}
		}

		ObjectMapper om = new ObjectMapper();
		try {
			return this.error("参数错误", om.writeValueAsString(fieldErrors), ConstantCode.PARAMTER_ERROR_CODE);
		} catch (JsonProcessingException e) {
			LOGGER.error("异常发生.", e);
			return this.error("参数错误", fieldErrors.toString(), ConstantCode.PARAMTER_ERROR_CODE);
		}
	}

	public boolean isSuccess() {
		return this.success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public long getCode() {
		return this.code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return this.content;
	}

	public void setMsg(String msg) {
		this.content = msg;
	}

	public Object getData() {
		return this.data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}
